﻿Public Class frm_tipoPago
    Dim accesoDatosTarjeta As New AccesoDatos("Tarjeta")

    Enum cerrar
        salir
        confirmar
    End Enum

    Dim esCambio As Boolean = False

    Public Property _esCambio() As Boolean
        Get
            Return Me.esCambio
        End Get
        Set(ByVal value As Boolean)
            Me.esCambio = value
        End Set
    End Property

    Dim accion As cerrar = cerrar.salir


    Private Sub frm_tipoPago_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarCombo(cmb_tarjeta, accesoDatosTarjeta.leerTabla("Tarjeta"), "idTarjeta", "nombre")
        cmb_tarjeta.SelectedIndex = -1
        cmb_tarjeta.Enabled = False
        rbt_tipoEfectivo.Checked = True
    End Sub

    Private Sub cmd_confirmar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_confirmar.Click
        If dgv_formaPago.Rows.Count > 0 Then
            If lbl_mostrarTotal.Text = 0 Then
                If MessageBox.Show("¿Desea confirmar la venta?", "Confirmacion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    If esCambio = False Then
                        accion = cerrar.confirmar
                        frm_venta.confirmarVenta(dgv_formaPago)
                        Me.Close()
                        frm_menu.Show()
                    Else
                        accion = cerrar.confirmar
                        frm_cambio.confirmarCambioMontoMayor(dgv_formaPago)
                        Me.Close()
                        frm_menu.Show()
                    End If

                Else
                    Me.Show()

                End If
            Else
                MessageBox.Show("Monto total no abonado", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("Agregue un pago a la lista", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Public Sub cargarMonto(ByVal monto As String)
        lbl_mostrarTotal.Text = monto
        txt_montoParcial.Text = monto
    End Sub

    Private Sub cargarCombo(ByRef combo As ComboBox, ByRef tablaACargar As Data.DataTable, ByVal indice As String, ByVal campo As String)
        combo.DataSource = Nothing
        combo.DataSource = tablaACargar
        combo.ValueMember = indice
        combo.DisplayMember = campo
    End Sub

    Private Sub frm_prod_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If accion = cerrar.salir Then
            frm_venta.Show()
        End If
    End Sub

    Private Sub rbt_tipoTarjeta_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbt_tipoTarjeta.CheckedChanged
        If rbt_tipoTarjeta.Checked = True Then
            cmb_tarjeta.Enabled = True
            txt_nroAuto.Enabled = True
        Else
            txt_nroAuto.Text = ""
            txt_nroAuto.Enabled = False
            cmb_tarjeta.SelectedIndex = -1
            cmb_tarjeta.Enabled = False
        End If
    End Sub

    Private Sub cmd_agregarPago_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_agregarPago.Click
        Dim tipoPago As String
        Dim idTipoPago As String
        Dim tarjeta As String
        Dim idTarjeta As String
        Dim monto As String
        Dim nroAuto As String

        If rbt_tipoEfectivo.Checked = True Then
            tipoPago = "efectivo"
            idTipoPago = "1"
        Else
            tipoPago = "tarjeta"
            idTipoPago = "2"
        End If

        tarjeta = cmb_tarjeta.Text
        idTarjeta = cmb_tarjeta.SelectedValue
        monto = txt_montoParcial.Text
        nroAuto = txt_nroAuto.Text
        If rbt_tipoTarjeta.Checked = True Then
            If cmb_tarjeta.SelectedIndex <> -1 And txt_nroAuto.Text <> "" Then
                dgv_formaPago.Rows.Add(tipoPago, tarjeta, monto, idTipoPago, idTarjeta, nroAuto)
                lbl_mostrarTotal.Text = lbl_mostrarTotal.Text - txt_montoParcial.Text
                txt_montoParcial.Text = lbl_mostrarTotal.Text
            Else
                MessageBox.Show("Seleccione una tarjeta e ingrese su número de verificación.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            End If

        Else
            lbl_mostrarTotal.Text = lbl_mostrarTotal.Text - txt_montoParcial.Text
            txt_montoParcial.Text = lbl_mostrarTotal.Text
            dgv_formaPago.Rows.Add(tipoPago, tarjeta, monto, idTipoPago, idTarjeta, nroAuto)
        End If

        If lbl_mostrarTotal.Text = 0 Then
            cmd_agregarPago.Enabled = False
        End If

    End Sub

    Private Sub txt_montoParcial_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_montoParcial.Leave
        If CInt(txt_montoParcial.Text) > CInt(lbl_mostrarTotal.Text) Then
            txt_montoParcial.Text = lbl_mostrarTotal.Text
        End If
    End Sub


    Private Sub cmd_quitarPago_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_quitarPago.Click
        If dgv_formaPago.Rows.Count = 0 Then

        Else
            lbl_mostrarTotal.Text = CInt(lbl_mostrarTotal.Text) + CInt(dgv_formaPago.CurrentRow.Cells(2).Value)
            txt_montoParcial.Text = lbl_mostrarTotal.Text
            dgv_formaPago.Rows.Remove(dgv_formaPago.CurrentRow)
            cmd_agregarPago.Enabled = True
        End If
    End Sub

    Private Sub cmd_cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_cancelar.Click
        If esCambio = True Then
            frm_cambio.Show()
            Me.Close()
        Else
            frm_venta.Show()
            Me.Close()
        End If
    End Sub

    Private Sub txt_nroAuto_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_nroAuto.Leave
        If Not IsNumeric(txt_nroAuto.Text) Then
            MessageBox.Show("Ingrese un número de verificación válido.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            txt_nroAuto.Text = ""
            txt_nroAuto.Focus()
        End If
    End Sub
End Class